package astar;

import java.util.List;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import entity.Position;

public class AStarView extends BasicGame
{
	protected double[][] _map;
	protected Image _niv1;
	protected Image _wall;
	protected List<Position> _astar;

	public AStarView(String title)
	{
		super(title);
	}

	@Override
	public void render(GameContainer arg0, Graphics arg1) throws SlickException {

		if (_astar != null)
		{
			for (Position p : _astar)
			{
				_niv1.draw(p.getX() * _niv1.getWidth(),
						p.getY() * _niv1.getHeight());
			}
		}
		for (int i = 0; i < _map[0].length; i++)
		{
			for (int j = 0; j < _map.length; j++)
			{
				if (_map[i][j] == -1)
				{
					_wall.draw(i * _wall.getWidth(), j * _wall.getHeight());
				}
			}
		}

	}

	@Override
	public void init(GameContainer arg0) throws SlickException {
		calculate();
		_niv1 = new Image("data/astar.bmp");
		_wall = new Image("data/wall.bmp");

	}

	@Override
	public void update(GameContainer gc, int arg1) throws SlickException {
		Input input = gc.getInput();
		if (input.isKeyPressed(Input.KEY_A))
		{
			calculate();
		}
	}

	public void calculate() {
		// init
		_map = new double[100][100];
		for (int i = 0; i < _map[0].length; i++)
		{
			for (int j = 0; j < _map.length; j++)
			{

				int rand = (int) (Math.random() * 5);
				if (rand == 0)
				{
					_map[i][j] = -1;
				}
				else
				{
					_map[i][j] = 3;
				}
			}
		}
		// calcul
		_astar = new AStar(new AStarMapDouble(_map)).searchPath(new Position(0, 0), new Position(50, 50));
	}

}
